//
//  BalanceDetailController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/11/24.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "BalanceDetailController.h"
#import "BalanceTableViewCell.h"
#import "BalanceModel.h"

@interface BalanceDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL loadMore;
@property (nonatomic, copy) NSString *nextP;
@end

@implementation BalanceDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavi];
    [self getHttpData];
    [self.view addSubview:self.tableView];
}

- (void)getNewData
{
    self.loadMore = NO;
    [self.dataArray removeAllObjects];
    [self getHttpData];
}

- (void)getMoreData
{
    if (self.loadMore) {
        [self getHttpData];
    }else
    {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
}

- (void)getHttpData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[NSNumber numberWithInteger:1] forKey:@"capitalType"];
    if (self.loadMore == YES) {
        [dict setValue:self.nextP forKey:@"nextPageInfo"];
    }
    [HttpRequest formPostPath:FlowList_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
        NSDictionary *data = Data[@"result"];
        NSArray *array = data[@"currentPageResult"];
        for (NSInteger i=0; i<array.count; i++) {
            BalanceModel *model = [BalanceModel mj_objectWithKeyValues:array[i]];
            [self.dataArray addObject:model];
        }
        self.loadMore = [data[@"hasMore"]boolValue];
        if (self.loadMore == YES) {
            self.nextP = [NSString stringWithFormat:@"%@",data[@"nextPageInfo"]];
            [self.tableView.mj_footer resetNoMoreData];
        }
        else
        {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    }];
}

#pragma mark -- table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BalanceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BalanceTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = indexPath.row%2 == 0 ?TotalWhite:UIColorFromHex(0xF9F9F9);
    cell.desLab.hidden = indexPath.row%2== 0 ? YES:NO;
    BalanceModel *model = self.dataArray[indexPath.row];
    [cell getModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"indexPath.row = %li",indexPath.row);
}

- (UITableView *)tableView
{
    if (_tableView == nil)
    {
        // Fallback on earlier versions
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,TopBarHeight, SCREENWIDTH, SCREENHEIGHT-TopBarHeight-BottomHeight) style:UITableViewStylePlain];
        self.tableView.backgroundColor = TotalWhite;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        [self.tableView registerClass:[BalanceTableViewCell class] forCellReuseIdentifier:@"BalanceTableViewCell"];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.rowHeight = 71;
        if (self.dataArray.count == 0) {
            //self.tableView.tableFooterView = [self setNoDataView];
        }else
        {
            self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        }
        [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.tableView withView:self];
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
         self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)setNavi
{
    self.bgNavView.hidden = YES;
    [self setNaviWith:@"余额明细"];
    UIView *view = (UIView *)[self.view viewWithTag:20];
    view.backgroundColor = goldColor;
}

@end
